import sys
import os
import requests
from colorama import  init,Fore,Back,Style
init(autoreset=True)
class Colored(object):

    def purple(s):
        return Fore.MAGENTA + s + Fore.RESET
    #  前景色:红色  背景色:默认
    def red(s):
        return Fore.RED + s + Fore.RESET

    #  前景色:绿色  背景色:默认
    def green(s):
        return Fore.GREEN + s + Fore.RESET

    #  前景色:黄色  背景色:默认
    def yellow(s):
        return Fore.YELLOW + s + Fore.RESET

    #  前景色:蓝色  背景色:默认
    def blue(s):
        return Fore.BLUE + s + Fore.RESET

    #  前景色:洋红色  背景色:默认
    def magenta(s):
        return Fore.MAGENTA + s + Fore.RESET

    #  前景色:青色  背景色:默认
    def cyan( s):
        return Fore.CYAN + s + Fore.RESET

    #  前景色:白色  背景色:默认
    def white(s):
        return Fore.WHITE + s + Fore.RESET

    #  前景色:黑色  背景色:默认
    def black(s):
        return Fore.BLACK

    #  前景色:白色  背景色:绿色
    def white_green(s):
        return Fore.WHITE + Back.GREEN + s

    def dave(s):
        return Style.BRIGHT + Fore.GREEN + s

color = Colored()

print(Colored.red("由于部分问题，可能无法正常更新，更多信息请访问https://flowus.cn/gsdocs/share/f5ecd650-7f48-4690-839a-e6e53843f81a#173a1d69-492b-4c78-aac5-630a1bdfd661"))
parameter = sys.argv
if len(parameter) > 2:
    parameter = parameter[1:-1]
elif len(parameter) > 1:
    parameter = [parameter[1]]
else:
    sys.exit(0)
with open('gs_update.ini', 'r',encoding='utf8') as f:
    update_file = f.read()
    update_file = update_file.split(",")
for i in update_file:
    if i.split("=")[0] == "server":
        server = i.split("=")[1]
        if i.split("=")[1] == "not":
            break
        else:
            if server == "not":
                break
            #print("更新服务器:" + server + "\n")
    elif i.split("=")[0] == "ver_n":
        ver = float(i.split("=")[1])
    elif i.split("=")[0] == "ver":
        ver_text = i.split("=")[1]
    elif i.split("=")[0] == "channel":
        channel = i.split("=")[1]
    elif i.split("=")[0] == "net":
        net = i.split("=")[1]
for r in parameter:
    if server == "not":
        print(Colored.red("警告:暂未设置更新服务器。"))
        break
    elif net == "False":
        print(Colored.red("您已经禁止了访问 GavaScript-Web Network 服务，因此无法使用gs_update。"))
        break
    elif r == "update":
        try:
            response = requests.get(server, timeout=10)  # 设置超时时间为10秒
            response.raise_for_status()  # 检查是否返回了有效的状态码
            if float(response.json()["version"][0]["version_number"]) > ver and response.json()["version"][0]["channel"] == channel:
                print(Colored.red("⚠ 需要更新\n") + Colored.green(ver_text) + " -> " + Colored.purple(response.json()["version"][0]["version"]) + "\n更新内容:" +response.json()["version"][0]["content"] + "\n更新链接:" + response.json()["version"][0]["file"] + Colored.red("\n注意:此版本正在开发中，这些内容可能包含致命错误，请谨慎使用！"))
            elif float(response.json()["version"][1]["version_number"]) > ver:
                print(Colored.red("⚠ 需要更新\n") + Colored.green(ver_text) + " -> " + Colored.purple(response.json()["version"][1]["version"]) + "\n更新内容:" + response.json()["version"][1]["content"] + "\n更新链接:" + response.json()["version"][1]["file"])
            else:
                print("已是最新版本")
        except Exception as err:
            print(Colored.red(f"发生了其他错误:\n  ") +  Colored.red(str(err)))
    elif r == "version":
        print(Colored.green("GavaScript版本:GavaScript " + ver_text))
        print(Colored.green("GavaScript-Web Network 更新 版本:v0 版本号:019321"))
    elif r == "off":
        print("请前往gs_update.ini，在'net'更改为False")